package tp.pr5;

import tp.pr5.items.Item;
import tp.pr5.items.ItemContainer;

/**
 *It represents a place in the city. Places are connected by streets according to the 4 compass directions, North, East, South and West. Every place has a name, a textual description about itself, a list of items contained in the place, and a tag to discover if the place is the space ship. The description and the list of items are displayed when the robot arrives at the place.
 *A place can represent the spaceship where the robot is safe. When the robot arrives at this place, the application is over.
 * @author VICTOR & HENAR
 * @see tp.pr1.Street
 */

public class Place implements PlaceInfo{

	private String name;
	private boolean isSpaceShip;
	private String description;
	private ItemContainer container;
	
	/**
	 * Creates the place. Initially the list of items is empty
	 * @param name Place name
	 * @param isSpaceShip Is it a spaceship?
	 * @param description Place description
	 */
	public Place(String name, boolean isSpaceShip, String description) {
		this.name = name;
		this.isSpaceShip = isSpaceShip;
		this.description = description;
		container = new ItemContainer();
	}
	
	@Override
	public boolean isSpaceship()
	{
		return this.isSpaceShip;
	}
	
	/**
	 * Overrides toString method. Returns the place name, its description and the list of items contained in the place
	 * @see Object.toString()
	 */
	public String toString()
	{
		String items = this.name + Constants.LINE_SEPARATOR + this.description + Constants.LINE_SEPARATOR ;
		if (this.container.numberOfItems() > 0)
			items+= Constants.MESSAGE_ITEMS_ROOM + this.container.toString();
		else
			items += Constants.MESSAGE_NO_ITEMS;
		return items;
	}
	
	/**
	 * Tries to pick an item characterized by a given identifier from the place. If the action was completed the item must be removed from the place.
	 * @param id - The identifier of the item
	 * @return The item of identifier id if it exists in the place. Otherwise the method returns null
	 */
	public Item pickItem(String id)
	{
		return (container.pickItem(id));
	}
	
	/**
	 * Tries to add an item to the place. The operation can fail (if the place already contains an item with the same name)
	 * @param it - The item to be added
	 * @return true if and only if the item can be added to the place, i.e., the place does not contain an item with the same name
	 */
	public boolean addItem(Item it)
	{
		return (container.addItem(it));
	}
	
	/**
	 * Checks if an item is in this place
	 * @param id Identifier of an item
	 * @return true if and only if the place contains the item identified by id
	 */
	public boolean existItem(String id)
	{
		return (this.container.containsItem(id));
	}
	
	@Override
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * Drop an item in this place. The operation can fail, returning false
	 * @param it The name of the item to be dropped.
	 * @return true if and only if the item is dropped in the place, i.e., an item with the same identifier does not exists in the place
	 */
	public boolean dropItem(Item it)
	{
		return (container.addItem(it));
	}

	@Override
	public String getDescription() 
	{
		return this.description;
	}
}